<?php
/**
 * Created by PhpStorm.
 * User: zys
 * Date: 2018/6/26
 * Time: 15:41
 */
namespace PhalconRest\Controllers;
use \PhalconRest\Exceptions\HTTPException;
use PhalconRest\Models\Product;
use PhalconRest\Models\Message;

class ProductController extends RESTController{


    private  $fields = ['itemid','catid','catidname','title','thumb','username'];


    public function get()
    {
        $getLimit  = $this->request->get('limit');        // get 取值
        $getOffset = $this->request->get('offset');
        $limit  = $getLimit  ? $getLimit  : $this->limit;   //  没传值  使用默认值
        $offset = $getOffset ? $getOffset : $this->offset;

        $result =  Product::find([
                                    'columns' =>  $this->fields,
                                    'limit'   =>  $limit,
                                    'offset'  =>  $offset,
                                    'order'   => "itemid desc"
                                   ]);
        $total = Product::count();
        $data['data']  = $result ? $result->toArray() : "";
        $data['total'] = $total;
        return $this->respond($data);
    }

    /**
     * @param $id
     */
    public function getOne($id)
    {
        $result =  Product::findFirst(["itemid=:itemid:",
                                        "columns" => $this->fields,
                                        'bind'    => [
                                              "itemid" => $id,
                                      ]]);
        $data = $result ? $result->toArray() : "";
        return $this->respond($data);

    }

    public function post(){
        return array('Post / stub');
    }

    public function delete($id){
        return array('Delete / stub');
    }

    public function put($id){
        return array('Put / stub');
    }

    public function patch($id){
        return array('Patch / stub');
    }



}